package com.burgeon.backend.EnterpriseWeChat.rest.integral;

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetTotCount extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetTotCount(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Integer getData(String empId,String integral) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,integral), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<String> vipList = new ArrayList<>();

//        if(jsonArray.length()>0){
//            for (int i = 0; i < jsonArray.length(); i++) {
//                vipList.add(jsonArray.getJSONArray(i).getString(0));
//            }
//        }
//        System.err.println(vipList.size());
        return new JSONArray(str).getJSONObject(0).getInt("count");
    }

    private MultiValueMap Data(String empId,String integral) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'ID'");//平均折扣

        params.put("columns", colums);
//        params.put("count","true");
        params.put("table", "C_VIP");//表名

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        expr1.put("column", "'HR_EMPLOYEE_ID1;ID'");
        String a = "'"+empId+"'";
        expr1.put("condition", a);

        expr2.put("column", "'FA_VIPACC_ID;INTEGRAL'");
        String b = "'>"+integral+"'";
        expr2.put("condition", b);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);


        params.put("params", paramss);
        HashMap orderby = new HashMap();

        orderby.put("column", "MODIFIEDDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);
//        System.err.println(params);

        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) {
        GetTotCount g = new GetTotCount("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("722","500"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
